import 'package:flutter/material.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/model/love_gallery_model.dart' as LGModel;
import 'package:love_app/providers/love_grallery.dart';
import 'package:love_app/routes/application.dart';
import 'package:provider/provider.dart';

BuildContext _context;
LoveGralleryProvider _loveGralleryProvider;
// 每个相册页面item结构
class EveryItem extends StatelessWidget {
  final LGModel.Data itemData;
  final Color textColor; // api获取的数据

  const EveryItem({Key key, this.itemData, this.textColor}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    _loveGralleryProvider = Provider.of<LoveGralleryProvider>(context, listen: false);
    _context = context;
    return Container(
      padding: EdgeInsets.only(
        bottom: Su().h(80),
        top: Su().h(80),
      ),
      decoration: BoxDecoration(
        border: Border.all(width: 0),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _itemLeft(),
          _rightItem(),
        ],
      ),
    );
  }

  // 左边的内容
  Widget _itemLeft() {
    StringTimeToObject time = StringTimeToObject.toMap(itemData.timeKey);
    return Container(
      // alignment: Alignment.topCenter,
      width: Su().w(163),
      // decoration: BoxDecoration(
      //   border: Border.all(width: 0),
      // ),
      child: Column(
        children: <Widget>[
          _itemLeftDay(time.day),
          _itemLeftDate(time.month, time.year),
        ],
      ),
    );
  }

  // // 计算左边item高
  // double _itemLeftHeight(){
  //   int itemLength = itemData.pictureData.length;
  //   double height = Su().h(171);
  //   if(itemLength % 3 == 0){
  //     return (itemLength ~/ 3 * height).toDouble();
  //   }
  //   return (((itemLength ~/ 3) + 1) * height).toDouble();
  // }

  // 左边内容的日期
  Widget _itemLeftDay(String day) {
    return Container(
      child: Text(
        day,
        style: Su().textStyle(textColor, 44),
      ),
    );
  }

  // 左边内容的月份和年份
  Widget _itemLeftDate(String month, String year) {
    return Container(
      child: Text(
        month + "  " + year,
        style: Su().textStyle(textColor, 24),
      ),
    );
  }

  // 右边的内容
  Widget _rightItem() {
    List<Widget> _warpChilden = _warpChildenList();
    return Container(
      width: Su().w(586),
      // decoration: BoxDecoration(border: Border.all(width: 0)),
      child: Wrap(
        alignment: WrapAlignment.start,
        spacing: Su().w(20), // 主轴(水平)方向间距
        runSpacing: Su().w(20), // 纵轴（垂直）方向间距
        crossAxisAlignment: WrapCrossAlignment.start, //
        children: _warpChilden,
      ),
    );
  }

  // 组成warp的所有组件
  List<Widget> _warpChildenList() {
    List<Widget> list = [];
    itemData.pictureData.forEach((item) {
      String iamgeUrl = item.thumbnail;
      String bigImage = item.origin;
      int index =  _loveGralleryProvider.addGraleryImageData(bigImage);
      list.add(_warpItem(iamgeUrl, index));
    });
    return list;
  }

  // 组成warp的每一个组件
  Widget _warpItem(String imageUrl, int index) {
    return InkWell(
      onTap: () {
        String indexNum = index.toString();
        Application.router.navigateTo(_context, "/showGralley?index=$indexNum");
      },
      child: Container(
        child: Image.network(
          imageUrl,
          width: Su().w(171),
          height: Su().w(171),
        ),
      ),
    );
  }
}

// 把字符串日期切割成object
class StringTimeToObject {
  final String time;

  StringTimeToObject(this.time);

  String year;
  String month;
  String day;

  StringTimeToObject.toMap(this.time) {
    List timeList = time.split("-");
    this.year = timeList[0];
    this.month = timeList[1];
    this.day = timeList[2];
  }
}
